// pages/product_detail/index.js
// 导入request请求工具类
import {getBaseUrl,requestUtil} from '../../utils/requestUtil.js';
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl:'http://localhost:8080/springbootm7qt4/',
    productObj:{},
    activeIndex:0,
    cart:[],
    collect:1,
    id:null,
  },

  QueryParams:{
    id:1
  },
  productInfo:{

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(options) {
      this.setData({
          id:options.id
      })
      this.getProductDetail(options.id)
  },
  //tab点击事件
  handleItemTap(e){
    const {index}=e.currentTarget.dataset;
    this.setData({
      activeIndex:index
    })
  },


  // 获取食品详情
  async getProductDetail(id){
    const result = await requestUtil({url: '/fuwuxinxi/info/'+id,method:"GET"});
    this.productInfo=result.data;
    console.log(this.productInfo.productSwiperImageList)
    this.setData({
      productObj:result.data
    })
  },
    //点击轮播图 放大预览
    handlePrevewImage(e){
      console.log(e.currentTarget.dataset.url)
      const urls=this.productInfo.productSwiperImageList.map(v=>v.image);
      console.log(urls)
      const current = e.currentTarget.dataset.url;
      wx.previewImage({
        current:urls[0],
        urls:urls,
      })
    },

  //点击立即购买
  handleBuy(){
   wx.navigateTo({
     url: '/pages/orderp/orderp?name='+this.data.productObj.fuwumingcheng+'&price='+this.data.productObj.fuwujiage+'&type='+this.data.productObj.fuwuleixing,
   })
  },
 
  async handleCollects(){
   
    const id = this.productInfo.id;
    console.log(this.productInfo.id)
    this.QueryParams.id = id;
    console.log(this.QueryParams);
    const res = await requestUtil({url: '/collect/del',data:this.QueryParams});
    console.log(res);
    wx.showToast({
      title:res.msg,
      icon:'success',
      mask:true
    })
    this.getProductDetail(this.data.id)
  },



  async handleCollect(){
   
    const id = this.productInfo.id;
    console.log(this.productInfo.id)
    this.QueryParams.id = id;
    console.log(this.QueryParams);
    const res = await requestUtil({url: '/collect/create',data:this.QueryParams});
    console.log(res);
    wx.showToast({
      title:res.msg,
      icon:'success',
      mask:true
    })
    this.getProductDetail(this.data.id)
  },

  async handleSize(){
    wx.navigateTo({
      url: '/pages/size/index',
    })
  },
 



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function() {
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})